package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.PcrResultDetailMapper;
import com.zhijian.medical.entity.po.PcrResultDetail;
import com.zhijian.medical.entity.vo.request.PcrResultDetailSearchVo;
import com.zhijian.medical.entity.vo.response.PcrResultDetailVo;
import com.zhijian.medical.service.PcrResultDetailService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 荧光PCR-结果明细业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-08-30 15:35:52
 */
@Slf4j
@Service
public class PcrResultDetailServiceImpl implements PcrResultDetailService {

    @Autowired
    private PcrResultDetailMapper mapper;

    @Override
    public PcrResultDetail selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public PcrResultDetailVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByPcrId(Long pcrId) {
        mapper.deleteByPcrId(pcrId);
    }

    @Override
    public void deleteByPcrIdAndDeviceId(Long pcrId, Long deviceId) {
        mapper.deleteByPcrIdAndDeviceId(pcrId, deviceId);
    }

    @Override
    public void deleteByPcrKzyId(Long pcrKzyId) {
        mapper.deleteByPcrKzyId(pcrKzyId);
    }

    @Override
    public Long insert(PcrResultDetail pcrResultDetail) {
        Date now = new Date();
        pcrResultDetail.setCreateTime(now);
        pcrResultDetail.setUpdateTime(now);
        return mapper.insert(pcrResultDetail) > 0 ? pcrResultDetail.getId() : 0L;
    }

    @Override
    public boolean update(PcrResultDetail pcrResultDetail) {
        Date now = new Date();
        pcrResultDetail.setUpdateTime(now);
        return mapper.updateByPrimaryKey(pcrResultDetail) != 0;
    }

    @Override
    public Integer count(PcrResultDetailSearchVo pcrResultDetailSearchVo) {
        return mapper.count(pcrResultDetailSearchVo);
    }

    @Override
    public List<PcrResultDetailVo> selectList(PcrResultDetailSearchVo pcrResultDetailSearchVo) {
        List<PcrResultDetailVo> pcrResultDetailVos = mapper.selectList(pcrResultDetailSearchVo);
        if (CollectionUtil.isEmpty(pcrResultDetailVos)) {
            return Collections.emptyList();
        }
        return pcrResultDetailVos.stream().sorted(PcrResultDetailVo::compareTo).collect(Collectors.toList());
    }

    @Override
    public Page<PcrResultDetailVo> selectPage(PcrResultDetailSearchVo pcrResultDetailSearchVo) {
        //先直接查总数
        int count = count(pcrResultDetailSearchVo);
        //分页
        Page<PcrResultDetailVo> page = new Page<>(true, count, pcrResultDetailSearchVo.getPageNum(), pcrResultDetailSearchVo.getPageSize());
        pcrResultDetailSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<PcrResultDetailVo> responseVos = this.selectList(pcrResultDetailSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<PcrResultDetail> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public List<String> selectKzLineFilePathListByBlh(String blh) {
        List<String> list = mapper.selectKzLineFilePathListByBlh(blh);
        if (CollectionUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
    }

}